<?php
class News extends CI_Controller
{
    var $data;
    
    public function __construct() 
    {
        parent::__construct();
        //content
        $this->data['title'] = 'Cổng thông tin, dữ liệu tài chính - chứng khoán Việt Nam - ndhinvestors.com';
        // banner 
        $this->data['head_banner'] = array(
            'TRANG CHỦ'             =>  site_url(),
            'KINH TẾ VĨ MÔ'         =>  site_url('ndh/kinh-te-vi-mo'),
            'NHẬN ĐỊNH CHUYÊN GIA'  =>  site_url('ndh/nhan-dinh-chuyen-gia'),
            'BÁO CÁO PHÂN TÍCH'     =>  site_url('ndh/bao-cao-phan-tich'),
            'NHẬN ĐỊNH THỊ TRƯỜNG'  =>  site_url('ndh/nhan-dinh-thi-truong'),
        );
        
        // footer banner 
        $this->data['footer_banner'] = array(               
            'KINH TẾ VĨ MÔ'         =>  site_url('ndh/kinh-te-vi-mo'),
            'NHẬN ĐỊNH CHUYÊN GIA'  =>  site_url('ndh/nhan-dinh-chuyen-gia'),
            'BÁO CÁO PHÂN TÍCH'     =>  site_url('ndh/bao-cao-phan-tich'),
            'NHẬN ĐỊNH THỊ TRƯỜNG'  =>  site_url('ndh/nhan-dinh-thi-truong'),
        );
    }
    
    function header()
    {
        $this->load->view('header',$this->data);
    }
    
    /*
     * Footer
     */
    function footer()
    {
        $this->load->view('footer');
    }
    
    function index()
    {
        $id_news = $this->uri->segment(4);
        
        // Tin mới nhất
        $this->load->model('Company_news_model');            

        // thong tin chi tiet cua tin tuc
        $this->data['detail_news'] = $this->Company_news_model->getNewById($id_news);  
        
        $company_id = $this->data['detail_news']['companies_id'];
        
        // tin tuc khac cua cong ty hien tai
        $this->data['other_news']  = $this->Company_news_model->getOtherNewForSameCompany($id_news,$this->data['detail_news']['companies_id']);
        
        // báo cáo phân tích
        $options = array('new_types_id' => '3');
        $order = array('modified_date' => 'desc');
        $this->data['analyze_reports'] = $this->Company_news_model->getAllData($options, $order, 0, 5);
        
        // Bảng tí giá chứng khoán
        $this->load->model('Company_finance_model');
        $this->data['stock_table'] = $this->Company_finance_model->getStockTable();
        
        // Nhà đâu tư
        $this->load->model('Company_investor_model');
        
        // Nhà đầu tư trong nước (type = 1)
        $options = array('investors_id' => 1);
        $this->data['investor_data'] = $this->Company_investor_model->getAllData($options);
        
        // Nhà đầu tư nước ngoài (type = 2)
        $options = array('investors_id' => 2);
        $this->data['investor_foreign'] = $this->Company_investor_model->getAllData($options);                        
        
        // cac thong tin lien quan den cong ty
        $this->load->model('Company_model');
        
        // thong tin co ban & ban lanh dao
        $company_info['basic'] = $this->Company_model->getData($company_id);    
        
        $this->load->model('Employee_model');
        
        $positions = $this->Employee_model->getPositionForCompany($company_id); 
        
        if(isset($positions))
        {
            $list_position = array();
            foreach($positions as $k => $v)
            {
                if(!isset($list_position[$v['position']]))    
                {
                    $list_position[$v['position']] = $v['id'];    
                }
            }
            
            $employees = array();
            foreach ($list_position as $k => $v)
            {
                $employees[$k]  =  $this->Employee_model->getEmployByPos($v,$company_id);        
            }
                    
            $company_info['position'] = $employees;    
        }
        
        // thong tin co ban & thong tin ky thuat
        $this->load->model('Report_model');
        $basic_reports = $this->Report_model->getBasicReportByCompany($company_id);
        
        $company_info['basic_report'] = $basic_reports;
        
        $finance_reports = $this->Report_model->getFinanceReportByCompany($company_id);
        $company_info['finance_report'] = $finance_reports;
        
        $this->data['company_info'] = $company_info;
        // tong hop cac thong tin lien quan den cong ty
        
        $this->load->view('detail_news.php', $this->data);
    }
    
    function company()
    {
        $company_id = $this->uri->segment(4);
        
        // Tin mới nhất
        $this->load->model('Company_news_model');            

        // thong tin chi tiet cua tin tuc
        $this->data['detail_news'] = $this->Company_news_model->getNewCompanyId($company_id);   
        
        $id_news = $this->data['detail_news']['id'];
        
        // tin tuc khac cua cong ty hien tai
        if($id_news > 0)
        {
            $this->data['other_news']  = $this->Company_news_model->getOtherNewForSameCompany($id_news,$this->data['detail_news']['companies_id']);   
        }
        
        // báo cáo phân tích
        $options = array('new_types_id' => '3');
        $order = array('modified_date' => 'desc');
        $this->data['analyze_reports'] = $this->Company_news_model->getAllData($options, $order, 0, 5);
        
        // Bảng tí giá chứng khoán
        $this->load->model('Company_finance_model');
        $this->data['stock_table'] = $this->Company_finance_model->getStockTable();
        
        // Nhà đâu tư
        $this->load->model('Company_investor_model');
        
        // Nhà đầu tư trong nước (type = 1)
        $options = array('investors_id' => 1);
        $this->data['investor_data'] = $this->Company_investor_model->getAllData($options);
        
        // Nhà đầu tư nước ngoài (type = 2)
        $options = array('investors_id' => 2);
        $this->data['investor_foreign'] = $this->Company_investor_model->getAllData($options);                        
        
        
        // cac thong tin lien quan den cong ty
        $this->load->model('Company_model');
        
        // thong tin co ban & ban lanh dao
        $company_info['basic'] = $this->Company_model->getData($company_id);    
        
        $this->load->model('Employee_model');
        
        $positions = $this->Employee_model->getPositionForCompany($company_id); 
        
        if(isset($positions))
        {
            $list_position = array();
            foreach($positions as $k => $v)
            {
                if(!isset($list_position[$v['position']]))    
                {
                    $list_position[$v['position']] = $v['id'];    
                }
            }
            
            $employees = array();
            foreach ($list_position as $k => $v)
            {
                $employees[$k]  =  $this->Employee_model->getEmployByPos($v,$company_id);        
            }
                    
            $company_info['position'] = $employees;    
        }
        
        // thong tin co ban & thong tin ky thuat
        $this->load->model('Report_model');
        $basic_reports = $this->Report_model->getBasicReportByCompany($company_id);
        
        $company_info['basic_report'] = $basic_reports;
        
        $finance_reports = $this->Report_model->getFinanceReportByCompany($company_id);
        $company_info['finance_report'] = $finance_reports;
                  
        $company_info['finance_basic'] = $this->Company_finance_model->getCompanyFinanceBasic($company_id);
        
        /*echo "<pre>";
        print_r($company_info['finance_basic']);
        echo "</pre>";           */             
        
        // tong hop cac thong tin lien quan den cong ty
        $this->data['company_info'] = $company_info;
        
        $this->load->view('detail_news.php', $this->data);    
    }
}
?>
